package com.sc.javaee.controller;

import com.sc.javaee.pojo.Cart;
import com.sc.javaee.pojo.Result;
import com.sc.javaee.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cs;
    @RequestMapping("/add")
    public Result add(@RequestBody Cart c){
        //首先要根据id判断购物车里是否有已存在的货物
        //有的话就根据货物增加1
        //如果没有就直接加入新的商品
      Cart cart = cs.select(c.getProductId());
      if(cart!=null && cart.getProductId()!=null){
          int i = cs.update(c);
          return new Result(1,"更新成功",i);
      }else{
          int i = cs.add(c);
          return new Result(1,"新增成功",i);
      }
    }
    @RequestMapping("/batchAdd")
    public Result selectiveAdd(@RequestBody Cart c){
        //首先要根据id判断购物车里是否有已存在的货物
        //有的话就根据货物数量增加
        //如果没有就直接加入新的商品
        Cart cart = cs.select(c.getProductId());
        if(cart!=null && cart.getProductId()!=null){
            int i = cs.batchUpdate(c);
            return new Result(1,"更新成功",i);
        }else{
            int i = cs.batchAdd(c);
            return new Result(1,"新增成功",i);
        }
    }
    @RequestMapping("/batchDel")
    public Result batchDelI(Integer[] ids){
        /*if (userId == null) {
            return new Result(0, "用户ID不能为空");
        }*/
        int i=cs.batchDel(ids);
        if (i>0) return new Result(1,"删除成功",i);
        return new Result(0,"删除失败");
    }
    //查询购物车商品列表
    @RequestMapping("/selectCart")
    public Result selectCart(){
        List<Cart> list =cs.selectCard();
        return new Result(1,"查询成功",list);
    }

}
